<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right" style="font-size: 16px;">
      <el-breadcrumb-item :to="{ path: '/sys-admin' }">
        <i class="el-icon-s-promotion"></i> 后台管理
      </el-breadcrumb-item>
      <el-breadcrumb-item>类别列表</el-breadcrumb-item>
      <el-breadcrumb-item v-for="item in history"><span v-text="item.name"></span></el-breadcrumb-item>
    </el-breadcrumb>

    <el-divider></el-divider>

    <el-table :data="tableData" border style="width: 100%">
      <el-table-column prop="id" label="ID" width="60" align="center"></el-table-column>
      <el-table-column label="LOGO" width="70" align="center">
        <template slot-scope="scope">
          <el-avatar :size="30" :src="scope.row.icon" fit="contain"></el-avatar>
        </template>
      </el-table-column>
      <el-table-column prop="name" label="名称" width="180" align="center"></el-table-column>
      <el-table-column prop="keywords" label="关键词列表" header-align="center" :show-overflow-tooltip="true"></el-table-column>
      <el-table-column prop="sort" label="排序序号" width="80" align="center"></el-table-column>
      <el-table-column label="是否启用" width="80" align="center">
        <template slot-scope="scope">
          <el-switch
                  @change="toggleEnable(scope.row)"
                  v-model="scope.row.enable"
                  :active-value="1"
                  :inactive-value="0"
                  active-color="#13ce66"
                  inactive-color="#999">
          </el-switch>
        </template>
      </el-table-column>
      <el-table-column label="显示在导航栏" width="120" align="center">
        <template slot-scope="scope">
          <el-switch
                  @change="toggleDisplay(scope.row)"
                  v-model="scope.row.isDisplay"
                  :active-value="1"
                  :inactive-value="0"
                  active-color="#13ce66"
                  inactive-color="#999">
          </el-switch>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="100" align="center">
        <template slot-scope="scope">
          <el-button type="primary" icon="el-icon-edit" size="mini" circle
                     @click="openEditDialog(scope.row)"></el-button>
          <el-button type="danger" icon="el-icon-delete" size="mini" circle
                     @click="openDeleteConfirm(scope.row)"></el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
  export default {
    data() {
      return {
        tableData: []
      }
    },
    methods: {
      toggleEnable(category) {
        console.log('你点击了【' + category.name + '】的【是否启用】开关控件，当前开关值：' + category.enable);
        let  enableText = ['禁用', '启用'];
        let url = 'http://localhost:9080/categories/' + category.id;
        if (category.enable == 1) {
          url += '/enable';
        } else {
          url += '/disable';
        }
        console.log('url = ' + url);

        this.axios.post(url).then((response) => {
          let responseBody = response.data;
          if (responseBody.state == 20000) {
            this.$message({
              message: '将【' + category.name + '】的启用状态改为【' + this.enableText[category.enable] + '】成功！',
              type: 'success'
            });
          } else {
            this.$message.error(responseBody.message);
          }
          this.loadCategoryList();
        });
      },
      toggleDisplay(category) {
        console.log('你点击了【' + category.name + '】的【是否显示在导航栏】开关控件，当前开关值：' + category.isDisplay);
        let displayText = ['不显示在导航栏', '显示在导航栏'];
        let url = 'http://localhost:9080/categories/' + category.id;
        if (category.isDisplay == 1) {
          url += '/display';
        } else {
          url += '/hidden';
        }
        console.log('url = ' + url);

        this.axios.post(url).then((response) => {
          let responseBody = response.data;
          if (responseBody.state == 20000) {
            this.$message({
              message: '将【' + category.name + '】的显示在导航栏状态改为【' + this.displayText[category.isDisplay] + '】成功！',
              type: 'success'
            });
          } else {
            this.$message.error(responseBody.message);
          }
          this.loadCategoryList();
        });
      },
      openEditDialog(category) {
        let title = '提示';
        let message = '您正在尝试编辑【' + category.id + '-' + category.name + '】的类别详情，抱歉，此功能尚未实现……';
        this.$alert(message, title, {
          confirmButtonText: '确定'
        });
      },
      openDeleteConfirm(category) {
        let title = '提示';
        let message = '此操作将永久删除【' + category.name + '】类别，是否继续？';
        this.$confirm(message, title, {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.handleDelete(category);
        }).catch(() => {
        });
      },
      handleDelete(category) {
        let url = 'http://localhost:9080/categories/' + category.id + '/delete';
        console.log('url = ' + url);

        this.axios.post(url).then((response) => {
          let responseBody = response.data;
          if (responseBody.state == 20000) {
            this.loadCategoryList();
          } else {
            this.$alert(responseBody.message, '操作失败', {
              confirmButtonText: '确定',
              callback: action => {
                this.loadCategoryList();
              }
            });
          }
        });
      },
      loadCategoryList() {
        console.log('loadCategoryList...');
        let url = 'http://localhost:9080/categories';
        console.log('url = ' + url);

        this.axios.get(url).then((response) => {
          let responseBody = response.data;
          this.tableData = responseBody.data;
        });
      }
    },
    mounted() {
      this.loadCategoryList();
    }
  }
</script>

<style scoped>
</style>